<template>
  <div class="gi_table_page">
    <GiTable
      title="美团账号管理"
      row-key="id"
      :data="dataList"
      :columns="columns"
      :loading="loading"
      :scroll="{ x: '100%', y: '100%', minWidth: 1000 }"
      :pagination="pagination"
      :disabled-tools="['size']"
      :disabled-column-keys="['name']"
      @refresh="search"
    >
      <template #toolbar-left>
        <GiForm v-model="queryForm" search :columns="queryFormColumns" size="medium" @search="search" @reset="reset"></GiForm>
      </template>
      <template #enable="{ record }">
        <span v-if="record.enable">
          <icon-check-circle-fill class="success" />
          <span>启用</span>
        </span>
        <span v-if="!record.enable">
          <icon-minus-circle-fill class="warning" />
          <span>禁用</span>
        </span>
      </template>
      <template #toolbar-right>
        <a-button v-permission="['cakerun:brandAccount:create']" type="primary" @click="onAdd">
          <template #icon><icon-plus /></template>
          <template #default>新增</template>
        </a-button>
        <a-button v-permission="['cakerun:brandAccount:export']" @click="onExport">
          <template #icon><icon-download /></template>
          <template #default>导出</template>
        </a-button>
      </template>
      <template #action="{ record }">
        <a-space>
          <a-link v-permission="['cakerun:brandAccount:get']" title="详情" @click="onDetail(record)">详情</a-link>
          <a-link v-permission="['cakerun:brandAccount:update']" title="修改" @click="onUpdate(record)">修改</a-link>
          <a-link
            v-permission="['cakerun:brandAccount:delete']"
            status="danger"
            :disabled="record.disabled"
            :title="record.disabled ? '不可删除' : '删除'"
            @click="onDelete(record)"
          >
            删除
          </a-link>
        </a-space>
      </template>
    </GiTable>

    <BrandAccountAddModal ref="BrandAccountAddModalRef" @save-success="search" />
    <BrandAccountDetailDrawer ref="BrandAccountDetailDrawerRef" />
  </div>
</template>

<script setup lang="ts">
import type { TableInstance } from '@arco-design/web-vue'
import BrandAccountAddModal from './BrandAccountAddModal.vue'
import BrandAccountDetailDrawer from './BrandAccountDetailDrawer.vue'
import { type BrandAccountQuery, type BrandAccountResp, deleteBrandAccount, exportBrandAccount, listBrandAccount } from '@/apis/cakerun/brandAccount'
import { useDownload, useTable } from '@/hooks'
import { isMobile } from '@/utils'
import has from '@/utils/has'
import type { ColumnItem } from '@/components/GiForm'
import { DisEnableBoolStatusList } from '@/constant/common'

defineOptions({ name: 'BrandAccount' })
const queryForm = reactive<BrandAccountQuery>({
  sort: ['id,desc'],
})
const queryFormColumns: ColumnItem[] = reactive([
  {
    type: 'input',
    label: '品牌',
    field: 'brand',
    span: { xs: 24, sm: 8, xxl: 8 },
    props: {
      placeholder: '品牌',
    },
  },
  {
    type: 'input',
    label: '账号',
    field: 'account',
    span: { xs: 24, sm: 8, xxl: 8 },
    props: {
      placeholder: '账号',
    },
  },
  {
    type: 'select',
    label: '状态',
    field: 'enable',
    span: { xs: 24, sm: 6, xxl: 8 },
    props: {
      options: DisEnableBoolStatusList,
      placeholder: '请选择状态',
    },
  },
])
const {
  tableData: dataList,
  loading,
  pagination,
  search,
  handleDelete,
} = useTable((page) => listBrandAccount({ ...queryForm, ...page }), { immediate: true })
const columns: TableInstance['columns'] = [
  { title: '品牌', dataIndex: 'brand', slotName: 'brand', width: 100 },
  { title: '地区', dataIndex: 'area', slotName: 'area', width: 100 },
  { title: '账号', dataIndex: 'account', slotName: 'account', width: 100 },
  { title: '密码', dataIndex: 'password', slotName: 'password', width: 100 },
  { title: '所属部门', dataIndex: 'deptName', slotName: 'deptName', width: 150 },
  { title: '所属人员', dataIndex: 'nickname', slotName: 'nickname', width: 150 },
  { title: '门店同步时间', dataIndex: 'orderShopTime', slotName: 'orderShopTime', width: 120 },
  { title: '项目同步时间', dataIndex: 'orderProjectTime', slotName: 'orderProjectTime', width: 120 },
  { title: '订单同步时间', dataIndex: 'orderDetailTime', slotName: 'orderDetailTime', width: 120 },
  { title: '评价同步时间', dataIndex: 'feedbackInfoTime', slotName: 'feedbackInfoTime', width: 120 },
  { title: '指标同步时间', dataIndex: 'storeMetricsTime', slotName: 'storeMetricsTime', width: 120 },
  { title: '创建时间', dataIndex: 'createTime', slotName: 'createTime', width: 230 },
  { title: '状态', dataIndex: 'enable', slotName: 'enable', width: 100 },
  {
    title: '操作',
    dataIndex: 'action',
    slotName: 'action',
    width: 160,
    align: 'center',
    fixed: !isMobile() ? 'right' : undefined,
    show: has.hasPermOr(['cakerun:brandAccount:get', 'cakerun:brandAccount:update', 'cakerun:brandAccount:delete'])
  },
]

// 重置
const reset = () => {
  search()
}

// 删除
const onDelete = (record: BrandAccountResp) => {
  return handleDelete(() => deleteBrandAccount(record.id), {
    content: `是否确定删除该条数据？`,
    showModal: true,
  })
}

// 导出
const onExport = () => {
  useDownload(() => exportBrandAccount(queryForm))
}

const BrandAccountAddModalRef = ref<InstanceType<typeof BrandAccountAddModal>>()
// 新增
const onAdd = () => {
  BrandAccountAddModalRef.value?.onAdd()
}

// 修改
const onUpdate = (record: BrandAccountResp) => {
  BrandAccountAddModalRef.value?.onUpdate(record.id)
}

const BrandAccountDetailDrawerRef = ref<InstanceType<typeof BrandAccountDetailDrawer>>()
// 详情
const onDetail = (record: BrandAccountResp) => {
  BrandAccountDetailDrawerRef.value?.onOpen(record.id)
}
</script>

<style scoped lang="scss"></style>
